﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using System.Security.Cryptography;

namespace PTTB
{
    class funtion
    {
        #region AddMdiChild
        /// <summary>
        /// Thêm một form vào MDI, nếu có thì hiện lên
        /// Nếu chưa có thì thêm mới và hiện
        /// </summary>
        /// <param name="parentFrm">Form MDI </param>
        /// <param name="childFrm">Form con</param>
        public static void AddMdiChild(Form parentFrm, Form childFrm)
        {
            Form[] childArr = parentFrm.MdiChildren;
            bool isExist = false;
            Form currentForm = null;
            foreach (Form f in childArr)
            {
                if (f.Name == childFrm.Name)
                {
                    isExist = true;
                    currentForm = f;
                    break;
                }
            }
            if (isExist == true)
                currentForm.Select();
            else
            {
                childFrm.MdiParent = parentFrm;
                childFrm.Show();
            }
        }
        #endregion
        #region ValidIsNotBlank
        public static ConditionValidationRule ValidIsNotBlank(string errortext)
        {
            ConditionValidationRule ret = new ConditionValidationRule { ConditionOperator = ConditionOperator.IsNotBlank, ErrorText = errortext };
            return ret;
        }
        #endregion
        #region ValidIsNotEqual
        public static CompareAgainstControlValidationRule ValidIsNotEqual(Control sosanh, string errortext)
        {
            CompareAgainstControlValidationRule compareValidationRule = new CompareAgainstControlValidationRule { Control = sosanh, CompareControlOperator = CompareControlOperator.Equals, ErrorText = errortext, CaseSensitive = true };
            return compareValidationRule;
        }
        #endregion

        #region MD5Encoded
        public static string MD5Encoded(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        #endregion

        #region RandomString
        public static string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }
        #endregion

        #region SetContainsFilter
        public static void SetContainsFilter(GridView gridview)
        {
            foreach (GridColumn col in gridview.Columns)
                col.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
        }
        #endregion
    }
}
